package com.tony.akka;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;

/**
 * @version V1.0  </br>
 * @ClassName Greeter </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 3/18/2019 10:16 AM</br>
 */
public class Greeter extends AbstractActor {
    private final ActorRef printerActor;
    private final String message;
    private String greeting = "";

    public Greeter(ActorRef pPrinterActor, String pMessage) {
        printerActor = pPrinterActor;
        message = pMessage;
    }

    static public Props props(String pMessage, ActorRef pPrinterActor) {
        return Props.create(Greeter.class, ()-> new Greeter(pPrinterActor,pMessage));
    }

    static public class WhoToGreet {
        public final String who;

        public WhoToGreet(String who) {
            this.who = who;
        }
    }
    static public class Greet {
        public Greet() {
        }
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(WhoToGreet.class, pWhoToGreet -> {
                    this.greeting = message + "," + pWhoToGreet.who;
                })
                .match(Greet.class, x -> {
                    printerActor.tell(new Printer.Greeting(greeting), getSelf());
                })
                .build();
    }
}
